package com.dommy.tab.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.dommy.tab.R;
import com.dommy.tab.adapter.MsgContentFragmentAdapter;
import com.dommy.tab.custom.CustomBarChart;
import com.dommy.tab.util.WaterUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 折线页
 */
public class FindFragment extends Fragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private LinearLayout customBarChart1;
    private MsgContentFragmentAdapter adapter;
    private List<String> names;
    String[] xLabel = {"0", "1", "2", "3", "4"};
    String[] yLabel = {"0", "25", "50"};
    Context context;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        ButterKnife.bind(this, view);
        customBarChart1 =  view.findViewById(R.id.customBarChart1);
        adapter = new MsgContentFragmentAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        context = inflater.getContext();
        // 更新适配器数据
        adapter.setList(names);
        initBarChart1();
        return view;
    }

    private void initData() {
        names = new ArrayList<>();
        names.add(" ");
    }



    /**
     * 初始化柱状图1数据
     */
    private void initBarChart1() {
        double[] data1 = {30, 50, 55, 50, 30};
        data1 = getData();
        List<double[]> data = new ArrayList<>();
        data.add(data1);
        List<Integer> color = new ArrayList<>();
        color.add(R.color.color12);
        color.add(R.color.color13);
        color.add(R.color.color16);
        customBarChart1.addView(new CustomBarChart(context, xLabel, yLabel, data, color));
    }


    public double[] getData(){
        double [] ints = WaterUtil.getDaysWater();
        setYValue(getValueMax(ints));
        return ints;
    }

    private void setYValue(int max) {
        yLabel[1] = max/2+"";
        yLabel[2] = max+"";
    }

    public int getValueMax(double[] doubles){
        int max = 50;
        for (double aDouble : doubles) {
            if(max < aDouble*2){
                max  = (int)aDouble*2;
            }
        }
        return max;
    }
}
